﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;

namespace WcfServiceDuplex
{
    public class ChartService : IChartService
    {
        public static Dictionary<IChartServiceCallback, ChatUser> dictCallbackToUser = new Dictionary<IChartServiceCallback, ChatUser>();

        public bool Join(ChatUser chatUser)
        {
            IChartServiceCallback client = OperationContext.Current.GetCallbackChannel<IChartServiceCallback>();

            if (dictCallbackToUser.ContainsValue(chatUser) == false)
            {
                dictCallbackToUser.Add(client, chatUser);
            }

            foreach (IChartServiceCallback callbackClient in dictCallbackToUser.Keys)
            {
                callbackClient.UpdateUsersList(dictCallbackToUser.Values.ToList());
            }

            return true;
        }

        public void Leave(ChatUser chatUser)
        {
            IChartServiceCallback client = OperationContext.Current.GetCallbackChannel<IChartServiceCallback>();
            if (dictCallbackToUser.ContainsValue(chatUser))
            {
                dictCallbackToUser.Remove(client);
            }

            foreach (IChartServiceCallback callbackClient in dictCallbackToUser.Keys)
            {
                if (chatUser.IsServer)
                {
                    if (callbackClient != client)
                    {
                        callbackClient.ServerDisconnected();
                    }
                }
                else
                {
                    callbackClient.UpdateUsersList(dictCallbackToUser.Values.ToList());
                }
            }
        }

        public void SendBroadcastMessage(string clientName, string message)
        {
            IChartServiceCallback client = OperationContext.Current.GetCallbackChannel<IChartServiceCallback>();
            if (client != null)
            {
                foreach (IChartServiceCallback callbackClient in dictCallbackToUser.Keys)
                {
                    if (callbackClient != OperationContext.Current.GetCallbackChannel<IChartServiceCallback>())
                    {
                        callbackClient.NotifyMessage(clientName + ":" + message);
                    }
                }
            }

        }

        public bool IsUserNameTaken(string strUserName)
        {
            foreach (ChatUser chatUser in dictCallbackToUser.Values)
            {
                if (chatUser.NickName.ToUpper().CompareTo(strUserName) == 0)
                {
                    return true;
                }
            }

            return false;
        }

        public void SendInkStrokes(MemoryStream memoryStream)
        {
            IChartServiceCallback client = OperationContext.Current.GetCallbackChannel<IChartServiceCallback>();
            foreach (IChartServiceCallback callbackClient in dictCallbackToUser.Keys)
            {
                if (callbackClient != OperationContext.Current.GetCallbackChannel<IChartServiceCallback>())
                {
                    callbackClient.OnInkStrokesUpdate(dictCallbackToUser[client], memoryStream.GetBuffer());
                }
            }

        }
    }
}
